package rexsee.ebook;

import java.io.File;

import rexsee.core.style.RexseeDrawable;
import rexsee.core.utilities.Utilities;
import rexsee.multimedia.RexseeImage;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Rect;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.Environment;

public class DrawableLine implements LineInterface {

	//private final Browser mBrowser;
	private final Context mContext;
	private final String mUrl;
	private final Rect mRect;
	private final DrawableGetter mGetter;

	public DrawableLine(Context context, String url, int width, int height, int maxWidth, int maxHeight, String cachePath, DrawableGetter getter) {
		mContext = context;
		mGetter = getter;
		Drawable mDrawable = (mGetter == null) ? RexseeDrawable.getDrawable(context, null, url) : mGetter.run(url);
		mRect = getScaledImageRect(mDrawable, width, height, maxWidth, maxHeight);
		if (mGetter != null) {
			mDrawable = null;
			mUrl = url;
		} else if (url.toLowerCase().startsWith("file://")) {
			mDrawable = null;
			mUrl = url;
		} else if (url.toLowerCase().startsWith("http://") || url.toLowerCase().startsWith("https://")) {
			Bitmap bitmap = ((BitmapDrawable) mDrawable).getBitmap();
			mDrawable = null;
			if (cachePath == null) cachePath = getCachePath();
			if (cachePath != null) {
				mUrl = cachePath + "/" + Utilities.md5(url, true) + ".png";
				File file = new File(Uri.parse(mUrl).getPath());
				if (!file.exists()) RexseeImage.saveBitmap(bitmap, 90, mUrl);
			} else {
				mUrl = url;
			}
			bitmap.recycle();
		} else {
			mUrl = url;
		}
	}
	private static String getCachePath() {
		if (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())) {
			return "file://" + Environment.getExternalStorageDirectory().getAbsolutePath() + "/ebookcache";
		} else {
			return null;
		}
	}

	@Override
	public boolean isNull() {
		return mUrl == null;
	}
	@Override
	public boolean isPageBreak() {
		return false;
	}
	@Override
	public int getHeight() {
		return mRect.height();
	}
	@Override
	public void draw(Canvas canvas, Rect rect) {
		if (isNull()) return;
		int left = rect.left + (rect.width() - mRect.width()) / 2;
		int top = rect.top + (rect.height() - mRect.height()) / 2;
		Drawable mDrawable = (mGetter == null) ? RexseeDrawable.getDrawable(mContext, null, mUrl) : mGetter.run(mUrl);
		mDrawable.setBounds(left, top, left + mRect.width(), top + mRect.height());
		mDrawable.draw(canvas);
	}
	public static Rect getScaledImageRect(Drawable imageDrawable, int maxWidth, int maxHeight) {
		int imageWidth = imageDrawable.getIntrinsicWidth();
		int imageHeight = imageDrawable.getIntrinsicHeight();
		if (imageWidth < 0 && imageHeight < 0) {
			return new Rect(0, 0, maxWidth, maxHeight);
		} else {
			float scale = Math.max(((float) imageWidth) / maxWidth, ((float) imageHeight) / maxHeight);
			if (scale > 1) {
				return new Rect(0, 0, Math.round(imageWidth / scale), Math.round(imageHeight / scale));
			} else {
				return new Rect(0, 0, imageWidth, imageHeight);
			}
		}
	}
	public static Rect getScaledImageRect(Drawable imageDrawable, int targetWidth, int targetHeight, int maxWidth, int maxHeight) {
		if (targetWidth < 0 && targetHeight < 0) {
			return getScaledImageRect(imageDrawable, maxWidth, maxHeight);
		} else if (targetWidth < 0) {
			return getScaledImageRect(imageDrawable, maxWidth, Math.min(targetHeight, maxHeight));
		} else if (targetHeight < 0) {
			return getScaledImageRect(imageDrawable, Math.min(targetWidth, maxWidth), maxHeight);
		} else {
			float scale = Math.max(((float) targetWidth) / maxWidth, ((float) targetHeight) / maxHeight);
			if (scale > 1) {
				return new Rect(0, 0, Math.round(targetWidth / scale), Math.round(targetHeight / scale));
			} else {
				return new Rect(0, 0, targetWidth, targetHeight);
			}
		}
	}
	@Override
	public String getIndexTitle() {
		return null;
	}
	@Override
	public String getText() {
		return null;
	}

	public static abstract class DrawableGetter {
		public abstract Drawable run(String url);
	}

}
